package org.softmed.rest.generation.builders;


import org.softmed.reflection.utils.FieldInfo;
import org.softmed.rest.config.Handler;
import org.softmed.rest.config.HttpMethod;
import org.softmed.rest.config.MimeTypeHandler;
import org.softmed.rest.config.Module;
import org.softmed.rest.config.Parameter;
import org.softmed.rest.config.Resource;
import org.softmed.rest.generation.FieldCreationParameters;
import org.softmed.rest.generation.HandlerSuite;
import org.softmed.rest.generation.RESTCreator;
import org.softmed.rest.generation.fields.GenerationConfig;


public class StringFieldCreator implements RESTCreator {

	// String groovyScriptDir = "services.administration";
	// String genericGetFieldTXTScript = ".generic_field_get_text";

	@Override
	public void createRESTConfig(FieldCreationParameters params) {

		Resource resource = createGetResourceFieldAsTXT(params.getModule(),
				params.getUri() + "/{" + params.getParameter() + "}/"
						+ params.getRestFieldName(), params.getParameter(),
				params.getType(), params.getFieldInfo().getName(), params
						.getProviderName(), params.getXmlSuite(), params
						.getHandlerSuite());

		createGenericSetFieldTextMethod(resource, "POST",
				params.getParameter(), params.getType(), params.getFieldInfo()
						.getName(), null, params.getProviderName(), params
						.getXmlSuite(), params.getHandlerSuite());

		createGenericSetFieldTextMethod(resource, "PUT", params.getParameter(),
				params.getType(), params.getFieldInfo().getName(), null, params
						.getProviderName(), params.getXmlSuite(), params
						.getHandlerSuite());

		createGenericDeleteFieldMethod(resource, params.getParameter(), params
				.getType(), params.getFieldInfo().getName(), params
				.getProviderName(), params.getXmlSuite(), params
				.getHandlerSuite());
	}

	public HttpMethod createGenericDeleteFieldMethod(Resource resource,
			String uriParameter, Class type, String fieldName,
			String providerName, String xmlSuite, HandlerSuite handlerSuite) {
		HttpMethod method;
		Handler handler;

		// delete
		method = new HttpMethod();
		method.setName("DELETE");
		method.setActive(true);

		resource.getHttpMethods().add(method);

		handler = new Handler();
		handler.setActive(true);

		// String handlerPath = GenericDeleteField.class.getCanonicalName();

		handler.setPath(handlerSuite.getGenericDeleteField());
		method.setHandler(handler);

		handler.getParameters().add(
				new Parameter("class", type.getCanonicalName()));
		handler.getParameters().add(new Parameter("parameter", uriParameter));
		handler.getParameters().add(new Parameter("field", fieldName));
		handler.getParameters().add(
				new Parameter("provider-name", providerName));
		handler.getParameters().add(new Parameter("xml-suite", xmlSuite));

		return method;

	}

	public HttpMethod createGenericSetFieldTextMethod(Resource resource,
			String httpMethod, String uriParameter, Class type,
			String fieldName, String fieldType, String providerName,
			String xmlSuite, HandlerSuite handlerSuite) {
		HttpMethod method;
		MimeTypeHandler mime;
		Handler handler;

		// get
		method = new HttpMethod();
		method.setName(httpMethod);
		method.setActive(true);

		resource.getHttpMethods().add(method);

		mime = new MimeTypeHandler();
		mime.setMimeType("text/plain");

		method.getMimeTypeHandlers().add(mime);

		handler = new Handler();
		handler.setActive(true);

		// String handlerPath = GenericChangeSimpleFieldText.class
		// .getCanonicalName();
		handler.setPath(handlerSuite.getGenericChangeSimpleFieldText());
		mime.setHandler(handler);

		handler.getParameters().add(
				new Parameter("class", type.getCanonicalName()));
		handler.getParameters().add(new Parameter("parameter", uriParameter));
		handler.getParameters().add(new Parameter("field", fieldName));
		handler.getParameters().add(
				new Parameter("provider-name", providerName));
		handler.getParameters().add(new Parameter("xml-suite", xmlSuite));
		if (fieldType != null)
			handler.getParameters().add(new Parameter("field-type", fieldType));

		return method;

	}

	public Resource createGetResourceFieldAsTXT(Module module,
			String targetURI, String parameter, Class type, String fieldName,
			String providerName, String xmlSuite, HandlerSuite handlerSuite) {
		Resource resource;
		resource = new Resource();
		resource.setActive(true);
		resource.setTargetUri(targetURI);
		module.getResources().add(resource);

		createGenericGetFieldTXTMethod(resource, parameter, type, fieldName,
				providerName, xmlSuite, handlerSuite);
		return resource;
	}

	public void createGenericGetFieldTXTMethod(Resource resource,
			String parameter, Class type, String fieldName,
			String providerName, String xmlSuite, HandlerSuite handlerSuite) {
		HttpMethod method;
		MimeTypeHandler mime;
		Handler handler;

		// get
		method = new HttpMethod();
		method.setName("GET");
		method.setActive(true);

		resource.getHttpMethods().add(method);

		mime = new MimeTypeHandler();
		mime.setMimeType("application/xml");

		method.getMimeTypeHandlers().add(mime);

		handler = new Handler();
		handler.setActive(true);
		handler.setPath(handlerSuite.getGenericFieldGetText());
		mime.setHandler(handler);

		handler.getParameters().add(
				new Parameter("class", type.getCanonicalName()));
		handler.getParameters().add(new Parameter("parameter", parameter));
		handler.getParameters().add(new Parameter("field", fieldName));
		handler.getParameters().add(
				new Parameter("provider-name", providerName));
		handler.getParameters().add(new Parameter("xml-suite", xmlSuite));
	}

}
